package net.realsee.service.wcs.warehouse.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.realsee.common.utils.StringUtils;
import net.realsee.domain.wcs.warehouse.WcsContainer;
import net.realsee.domain.wcs.warehouse.bo.WcsContainerBo;
import net.realsee.domain.wcs.warehouse.vo.WcsContainerVo;
import net.realsee.framework.core.domain.PageQuery;
import net.realsee.framework.core.page.TableDataInfo;
import net.realsee.mapper.wcs.warehouse.WcsContainerMapper;
import net.realsee.service.wcs.warehouse.IWcsContainerService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 容器Service业务层处理
 *
 * @author realsee
 * @date 2023-08-11
 */
@RequiredArgsConstructor
@Service
public class WcsContainerServiceImpl extends ServiceImpl<WcsContainerMapper, WcsContainer> implements IWcsContainerService {

    private final WcsContainerMapper baseMapper;

    /**
     * 查询容器
     */
    @Override
    public WcsContainerVo selectVoById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询容器列表
     */
    @Override
    public TableDataInfo<WcsContainerVo> selectVoPage(WcsContainerBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WcsContainer> lqw = buildVoQueryWrapper(bo);
        Page<WcsContainerVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询容器列表
     */
    @Override
    public List<WcsContainerVo> selectVoList(WcsContainerBo bo) {
        LambdaQueryWrapper<WcsContainer> lqw = buildVoQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WcsContainer> buildVoQueryWrapper(WcsContainerBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WcsContainer> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, WcsContainer::getId, bo.getId());
        lqw.eq(StringUtils.isNotBlank(bo.getContainer()), WcsContainer::getContainer, bo.getContainer());
        lqw.eq(StringUtils.isNotBlank(bo.getContainerType()), WcsContainer::getContainerType, bo.getContainerType());
        lqw.eq(bo.getStatus() != null, WcsContainer::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getCurrLocation()), WcsContainer::getCurrLocation, bo.getCurrLocation());
        lqw.eq(StringUtils.isNotBlank(bo.getCurrOrderno()), WcsContainer::getCurrOrderno, bo.getCurrOrderno());
        lqw.eq(StringUtils.isNotBlank(bo.getDefaultLocation()), WcsContainer::getDefaultLocation, bo.getDefaultLocation());
        lqw.eq(StringUtils.isNotBlank(bo.getFuid()), WcsContainer::getFuid, bo.getFuid());
        lqw.eq(bo.getTextId() != null, WcsContainer::getTextId, bo.getTextId());
        lqw.eq(bo.getRefrenceId() != null, WcsContainer::getRefrenceId, bo.getRefrenceId());
        lqw.eq(bo.getUnitId() != null, WcsContainer::getUnitId, bo.getUnitId());
        lqw.eq(bo.getDelFlag() != null, WcsContainer::getDelFlag, bo.getDelFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), WcsContainer::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getCreateTime() != null, WcsContainer::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), WcsContainer::getUpdateBy, bo.getUpdateBy());
        lqw.eq(bo.getUpdateTime() != null, WcsContainer::getUpdateTime, bo.getUpdateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getDeleteBy()), WcsContainer::getDeleteBy, bo.getDeleteBy());
        lqw.eq(bo.getDeleteTime() != null, WcsContainer::getDeleteTime, bo.getDeleteTime());
        lqw.eq(bo.getRowVersionStamp() != null, WcsContainer::getRowVersionStamp, bo.getRowVersionStamp());
        lqw.eq(StringUtils.isNotBlank(bo.getRemark()), WcsContainer::getRemark, bo.getRemark());
        return lqw;
    }

    /**
     * 新增容器
     */
    @Override
    public Boolean insertByBo(WcsContainerBo bo) {
        WcsContainer add = BeanUtil.toBean(bo, WcsContainer.class);
        return this.save(add);
    }

    /**
     * 修改容器
     */
    @Override
    public Boolean updateByBo(WcsContainerBo bo) {
        WcsContainer update = BeanUtil.toBean(bo, WcsContainer.class);
        return this.updateById(update);
    }

    /**
     * 批量删除容器
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        return this.removeBatchByIds(ids);
    }
}
